package lib

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
	"sync"
	"golang.org/x/crypto/bcrypt"
)

var engine *xorm.Engine
var once sync.Once

type Helper struct {

}
func GetDbEngine () *xorm.Engine {

	once.Do(func() {
		var err error
		engine, err = xorm.NewEngine("mysql", "root:814876569@/gochat?charset=utf8")
		if err != nil {
			log.Fatal("mysql error")
		}
	})
	return engine
}
func PasswordBcrypt (pwd string) string {
	password := []byte(pwd)
	hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
	if err != nil {
		panic(err)
	}
	return string(hashedPassword)
}
func ComparePassword (hashedPassword string, password string) bool{
	err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
	if err != nil  {
		return false
	} else {
		return true
	}
}